
 /*
author:
time : 2021年10月26日
 */ 
<template>
  <div>

    <!-- 管理员页面 -->

    <!-- 搜索框 -->
    <div style="margin-top: 15px">
      <el-input placeholder="请输入内容" v-model="input" class="input">
        
      </el-input>
      <el-button
          class="btn"
          slot="append"
          icon="el-icon-search"
          @click="search"
        ></el-button>
      <button class="add" type="buttom" @click="add()">
        <i class="icon el-icon-plus"></i> 增加管理员
      </button>
    </div>
    <!-- 表格 -->
    <div class="content_table">
      <el-table :data="adminList" style="width: 99%">
        <el-table-column :width="200" label="ID">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column  label="昵称">
          <template slot-scope="scope">
            <span>{{ scope.row.nickname }}</span>
          </template>
        </el-table-column>
        <el-table-column  label="用户名">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
          <el-table-column  label="管理员身份">
          <template slot-scope="scope">
            <span>{{ scope.row.jurisdiction }}</span>
          </template>
        </el-table-column>
        <el-table-column :width="400" label="头像">
          <template slot-scope="scope">
            <div slot="reference" class="name-wrapper">
              <!-- <span>{{ scope.row.headPortrait }}</span> -->
               <img :src="`${imageName}${scope.row.headPortrait}`" alt="">
            </div>
          </template>
        </el-table-column>
        <el-table-column  label="操作" :width="250" >
          <template slot-scope="scope">
           
            <el-button size="mini" @click="edit(scope.row)">编辑</el-button>
             <el-popconfirm
                  confirm-button-text='确定'
                  cancel-button-text='取消'
                  icon="el-icon-info"
                  icon-color="red"
                  title="确定删除这一条数据吗？"
                  @onConfirm="del(scope.row.id)"
                >
            <el-button slot="reference" size="mini" type="danger" >删除</el-button>
          </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- <Page :page=page :count=count @pageChange=pageChange /> -->
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="page"
        :page-size="pageSize"
        layout="total, prev, pager, next,jumper"
        :total="count"
      >
      </el-pagination>
    </div>

    <!-- 模态框 -->
    <el-dialog title="添加" :visible.sync="dialogFormVisible" width="800px">
      <el-form :model="form">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称" :label-width="formLabelWidth">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input
            v-model="form.password"
            type="password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="头像" :label-width="formLabelWidth">
            <div class="uplod">
            <el-upload
              class="upload-demo"
              action="http://192.168.3.180:7001/api/uploadPicture"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
            <!-- <span id="photo"
              ><img :src="`${imageName}${imageUrl}${form.headPortrait}`"
            /></span> -->
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="sbumit()">提 交</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
// import Page from "@/components/Page.vue";
import {
  getAdminSer,
  getAdminAdd,
  getAdminEdit,
  getAdminDel,
} from "@/api/table";

export default {

  components: {

    // Page
  },
  data() {
    return {
      input: "",

      value: "100",

      // 是否启用,默认启用
      value1: true,
      adminList: [],
      //查询关键字
      keyName: "",
      // 数据库总页数
      count: 0,
      // 当前页数
      page: 1,
      // 每页显示几条数据
      pageSize: 5,
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        id: null,
        name: "",
        nickname: "",
        password: "",
        headPortrait:"",
      },
       imageUrl: "",
      imageName: "http://192.168.3.180:7001/",
      // 模态框输入长度
      formLabelWidth: "100px",
      // 判断是新增加还是修改,默认是添加
      isEdit: false,
    };
  },

  created() {
    this.getDate()
  },

  methods: {
    handleCurrentChange(){
      console.log("adad",this.count,this.page);
      this.getDate()
      
    },
      handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
      this.form.headPortrait = file.response.data;
      console.log(file.response.data);
    },
    // 获取管理员数据的方法
    getDate() {
      console.log(2222);
      console.log(this.page);
      const params = {
        page: this.page,
        number: this.pageSize,
        keyname: this.keyName,
      };
      getAdminSer(params).then((res) => {
        console.log(res);
        if (res.code === 0) {
          this.adminList = res.data;
          this.count = res.count;
          this._time(this.adminList);
        }
        if (res.code === 4004) {
          this.adminList = res.data;
          this.count = res.count;
          this.page=res.page;
        } 
      });
    },

    // 根据关键字查询
    search() {
      this.keyName = this.input;
      this.getDate();
    },
    // 根据权限判断管理员
    _time(list){
      for(const item of list){
        if(item.jurisdiction === '1'){
          item.jurisdiction='超级管理员';
        }else if(item.jurisdiction==='2'){
          item.jurisdiction='普通管理员'
        }else{
          item.jurisdiction='禁用管理员'
        }
      }
    },
    // 新增管理员
    add() {
      // 显示模态框
      this.dialogFormVisible = true;
      this.isEdit = true;
    },
    // 编辑管理员
    edit(data) {
      // 显示模态框
      this.dialogFormVisible = true;
      this.isEdit = false;
      this.form.id = data.id;
      this.form.name = data.name;
      this.form.nickname = data.nickname;
      this.form.password = "";
      this.form.headPortrait = data.headPortrait;

      // this.form.jurisdiction = data.jurisdiction;
    },
    close(){
      this.dialogFormVisible = false;
       this.form.id = "";
      this.form.name ="";
      this.form.nickname ="";
      this.form.password = "";
      this.form.headPortrait = "";
    },
    // 删除管理员
    del(id) {
  
          getAdminDel(id).then((res) => {
            if (res.code === 0) {
          
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              // 删除后重新渲染
              this.getDate();
            }
        })
   
    },
    // 点击提交
    sbumit() {
      // 添加到数据库
      let params = {};
      if (this.isEdit) {
        params = { ...this.form };
        console.log(params);
        getAdminAdd(params).then((res) => {
          if (res.code === 0) {
            this.$message({
              message: "添加管理员成功",
              type: "success",
            });
            // 添加成功后关闭模态框
            this.dialogFormVisible = false;
            // 把新添加的数据渲染到页面
            this.getDate();
          } else {
            this.$message.error("添加管理员失败");
          }
        });
      } else {
        params.id = this.form.id;
        params.name = this.form.name;
        params.nickname = this.form.nickname;
        params.password = this.form.password;
        params.headPortrait = this.form.headPortrait;

        // params.jurisdiction = this.form.jurisdiction;
        getAdminEdit(params).then((res) => {
          if (res.code === 0) {
            this.$message({
              message: "编辑管理员成功",
              type: "success",
            });
            // 添加成功后关闭模态框
            this.dialogFormVisible = false;
            // 把新添加的数据渲染到页面
            this.getDate();
          } else {
            this.$message.error("编辑管理员失败");
          }
        });
      }
    },
    // 分页
    pageChange(page) {
      console.log(page);
      this.page = page;
      this.getDate();
    },

  },
};
</script>

<style lang="scss" scoped>

.fa {
  margin-right: 5px;
}

.bread {
  width: 100%;
  height: 30px;
  padding: 10px 0;
  display: flex;
  padding-left: 10px;
  align-items: center;
  .el-breadcrumb {
    line-height: 100px;
    width: 100%;
    background-color: #fff;
  }
}
.input {
  background-color: #fff;
  width: 450px;
  margin: 30px 0px 10px 90px;

}
.add {
  background-color: rgb(133, 190, 228);
  color: white;
  border: none;
  line-height: 38px;
  border-radius: 5px;
  margin-left: 10px;
}
.content_table {
  width: 90%;
  // margin-top: 20px;
  // padding-left: 50px;
  // padding-right: 400px;
 margin: 10px 90px;
}
.el-button{
  margin-right: 10px;
}
.block{
  margin:10px 35px;
}
.el-form-item{
 margin-left: -50px;
 el-input{
   width: 300px;
   height: 30px;
   
 }
 
}
.el-pagination {
  padding: 5px 50px;
}
 img{
    width: 70px;
    height: 70px;
  }

</style> 